#include "IO.hh"

#include <iostream>
#include <fstream>
#include <cstdlib>

Concert* read ( std::string const& filename ) {
  std::ifstream ifs ( filename.c_str() );

  if ( !ifs ) {
    std::cerr << "File not found !" << std::endl;
    exit(0);
  }

  unsigned int mCount = 0; ifs >> mCount;
  unsigned int pCount = 0; ifs >> pCount;
  Concert* result = new Concert(mCount, pCount);

  for ( unsigned int m = 0; m < mCount; ++m ) {
    for ( unsigned int p = 0; p < pCount; ++p ) {
      unsigned int doesPlay = 0; ifs >> doesPlay;
      
      if ( doesPlay == 1 ){
      	result->musicians[m]->pieces.push_back( result->pieces[p] );
        result->pieces[p]->musicians.push_back( result->musicians[m] );
      }
    }

    unsigned int c = 0; ifs >> c;
    result->musicians[m]->id = m;
    result->musicians[m]->hourlyRate = c;
  }

  for ( unsigned int p = 0; p < pCount; ++p ) {
    unsigned int l = 0; ifs >> l;
    result->pieces[p]->id = p;
    result->pieces[p]->length= l;
    result->totalTime += l;
  }

  return result;
}
